package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.SelectedSalt;

public class SelectedSaltPresenter extends AbstractPresenter<SelectedSalt> {
	final String SINGLEITEM = "selectedSalt";
	final String MULTIITEM = "selectedSalts";
	
	public ArrayList<SelectedSalt> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public SelectedSalt tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public SelectedSalt createItemFromJSONObject(JSONObject jsonObject){
		SelectedSalt item = new SelectedSalt();
		try{
			item.setId(jsonObject.getInt("selectedSaltID"));
			item.setGrams(jsonObject.getDouble("selectedSaltGrams"));
			item.setNotes(jsonObject.getString("selectedSaltNotes"));
			WaterSaltsPresenter presenter = new WaterSaltsPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("waterSaltID");
			item.setWaterSalt(presenter.createItemFromJSONObject(jsonGroup));
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(SelectedSalt item){
		try{
			JSONObject json = new JSONObject();
			json.put("selectedSaltID", item.getId());
			json.put("selectedSaltGrams", item.getGrams());
			json.put("selectedSaltNotes", item.getNotes());
			WaterSaltsPresenter presenter = new WaterSaltsPresenter();
			json.put("waterSaltID", presenter.createJSON(item.getWaterSalt()));
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public SelectedSalt getEmptyEntity(){
		SelectedSalt entity = new SelectedSalt();
		return entity;
	}
}
